//
//  UIColor+GNExtension.m
//  GN100
//  Created by apple on 15/9/8.
//  Copyright (c) 2015年 sunfei. All rights reserved.
//

#import "UIColor+GNExtension.h"
#import <UIKit/UIKit.h>
#import <objc/runtime.h>
@implementation UIColor (GNExtension)
//全局函数
void dynamicMethodIMP(id self, SEL _cmd)
{
    
}
+ (BOOL) resolveInstanceMethod:(SEL)aSEL
{
    if (aSEL == @selector(resolveThisMethodDynamically))
    {
        class_addMethod([self class], aSEL, (IMP) dynamicMethodIMP, "v@:");
        return YES;
    }
    return [super resolveInstanceMethod:aSEL];
}
+(void)initialize
{
    
}
+(void)load
{
    //    //转换颜色色值
    //    + (UIColor *)colorWithRGBColorByString:(NSString *)stringToConvert
    //    {
    //        NSString *cString = [[stringToConvert stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    //
    //        // String should be 6 or 8 characters
    //        if ([cString length] < 6) return [UIColor whiteColor];
    //
    //        // strip 0X if it appears
    //        if ([cString hasPrefix:@"0X"]) cString = [cString substringFromIndex:2];
    //        else if ([cString hasPrefix:@"#"]) cString = [cString substringFromIndex:1];
    //        if ([cString length] != 6) return [UIColor whiteColor];
    //        // Separate into r, g, b substrings
    //        NSRange range;
    //        range.location = 0;
    //        range.length = 2;
    //        NSString *rString = [cString substringWithRange:range];
    //
    //        range.location = 2;
    //        NSString *gString = [cString substringWithRange:range];
    //
    //        range.location = 4;
    //        NSString *bString = [cString substringWithRange:range];
    //
    //        // Scan values
    //        unsigned int r, g, b;
    //        [[NSScanner scannerWithString:rString] scanHexInt:&r];
    //        [[NSScanner scannerWithString:gString] scanHexInt:&g];
    //        [[NSScanner scannerWithString:bString] scanHexInt:&b];
    //
    //        return [UIColor colorWithRed:((float) r / 255.0f)
    //                               green:((float) g / 255.0f)
    //                                blue:((float) b / 255.0f)
    //                               alpha:1.0f];
    //    }
    //
}
/**
 *
 *  传入16进制自动转换RGB颜色
 *
 *  @param stringToConvert 传入16进制颜色字符串
 *
 *  @return 返回RGB颜色
 */
+ (UIColor *)colorWithRGBColorByString:(NSString *)stringToConvert
{
    NSString *cString = [[stringToConvert stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    
    // String should be 6 or 8 characters
    if ([cString length] < 6) return [UIColor whiteColor];
    
    // strip 0X if it appears
    if ([cString hasPrefix:@"0X"]) cString = [cString substringFromIndex:2];
    else if ([cString hasPrefix:@"#"]) cString = [cString substringFromIndex:1];
    if ([cString length] != 6) return [UIColor whiteColor];
    // Separate into r, g, b substrings
    NSRange range;
    range.location = 0;
    range.length = 2;
    NSString *rString = [cString substringWithRange:range];
    
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    
    // Scan values
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    
    return [UIColor colorWithRed:((float) r / 255.0f)
                           green:((float) g / 255.0f)
                            blue:((float) b / 255.0f)
                           alpha:1.0f];
}

+ (UIColor *)colorWithRGBColorByString1:(NSString *)stringToConvert
{
    NSString *cString = [[stringToConvert stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    
    // String should be 6 or 8 characters
    if ([cString length] < 6) return [UIColor whiteColor];
    
    // strip 0X if it appears
    if ([cString hasPrefix:@"0X"]) cString = [cString substringFromIndex:2];
    else if ([cString hasPrefix:@"#"]) cString = [cString substringFromIndex:1];
    if ([cString length] != 6) return [UIColor whiteColor];
    // Separate into r, g, b substrings
    NSRange range;
    range.location = 0;
    range.length = 2;
    NSString *rString = [cString substringWithRange:range];
    
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    
    // Scan values
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    
    return [UIColor colorWithRed:((float) r / 255.0f)
                           green:((float) g / 255.0f)
                            blue:((float) b / 255.0f)
                           alpha:0.5f];
}
/**
 * 生成随机色
 *
 *  @return
 */
+(UIColor *)colorWithRandomColor
{
    CGFloat hue = ( arc4random() % 256 / 256.0 ); //0.0 to 1.0
    CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0,away from white
    CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5; //0.5 to 1.0,away from black
    return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
}
@end
